package com.huaxin.common.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.UserManager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.huaxin.common.tts.Constant;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @author songxf
 * @date 2017/11/8
 * @Desc 常用工具方法
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxUtils {
    //手机号表达式
    private final static Pattern phone_pattern = Pattern.compile("^(13|15|18)\\d{9}$");

    /**
     * 验证手机号是否正确
     *
     * @param phone 手机号码
     * @return boolean
     */
    public static boolean isPhone(String phone) {
        return phone_pattern.matcher(phone).matches();
    }

    /**
     * 测量View的宽高
     *
     * @param view View
     */
    public static void measureWidthAndHeight(View view) {
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentTime() {
        return getFormatDateTime(new Date(), "HH:mm:ss");
    }


    /**
     * 根据给定的格式与时间(Date类型的)，返回时间字符串<br>
     *
     * @param date   指定的日期
     * @param format 日期格式字符串
     * @return String 指定格式的日期字符串.
     */
    public static String getFormatDateTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 获取年-月-日
     *
     * @return
     */
    public static String getYearMonthDay() {
        return getYear() + "-" + getMonth() + "-" + getDay();
    }

    /**
     * 获取年-月
     *
     * @return
     */
    public static String getYearMonth() {
        return getYear() + "-" + getMonth();
    }

    /**
     * 获取月
     *
     * @return
     */
    public static int getDay() {
        return getCalender().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取月
     *
     * @return
     */
    public static int getMonth() {
        return getCalender().get(Calendar.MONTH) + 1;
    }

    /**
     * 获取年
     *
     * @return
     */
    public static int getYear() {
        return getCalender().get(Calendar.YEAR);
    }

    private static Calendar getCalender() {
        return Calendar.getInstance();
    }

    /**
     * 根据规则得到时间戳
     * 模式 hh:mm:ss
     *
     * @param realStartDate 时间戳
     * @return
     */
    public static String getPatternToTimes(String realStartDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(new Date(toLong(realStartDate)));
    }

    private static long toLong(String obj) {
        try {
            return Long.parseLong(obj);
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 根据字符串转成day
     *
     * @param date
     * @return
     */
    public static int getStringToDay(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = sdf.parse(date);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            return cal.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    public static int getWindowWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    /*
   * 获取当前程序的版本号
   */
    public static String getVersionName(Context context) {
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo;
        String version = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            version = packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return version;
    }

    /**
     * 判断是否连接wifi
     *
     * @param mContext
     * @return
     */
    public static boolean isWifi(Context mContext) {
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否是首创平台
     *
     * @param orgCode
     * @return
     */
    public static boolean isScOrg(String orgCode) {
        if (Constant.SCOrgcodeFirst.equals(orgCode)) {
            return true;
        } else {
            return false;
        }
    }

    public static String getTruncationStr(String falg) {
        if (!TextUtils.isEmpty(falg)) {
            return falg.substring(1, falg.length() - 1);
        }
        return "";
    }


}

